package com.example.demo.service.Impl;


import com.example.demo.mapper.PunchRecordMapper;
import com.example.demo.model.PunchRecord;
import com.example.demo.service.PunchRecordService;
import com.example.demo.service.WeLinkIntegrationService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;

@Service
@Transactional(rollbackFor = Exception.class)
public class PunchCardServiceImpl  implements PunchRecordService {

    private static final Logger log = LoggerFactory.getLogger(PunchRecordService.class);

    @Resource
    private PunchRecordMapper punchRecordMapper;

    @Resource
    private WeLinkIntegrationService weLinkIntegrationService;

    /**
     * 处理打卡机打卡数据
     *
     * @param record
     * @throws IOException
     * @throws InterruptedException
     */
    public void processPunchRecord(PunchRecord record) throws IOException, InterruptedException {
        // 校验数据
        if (record.getUserAccount() == null || record.getPunchCardTime() == null || record.getWelinkId() == null) {
            throw new IllegalArgumentException("打卡数据无效");
        }
        // 将打卡机打卡数据保存到数据库
        punchRecordMapper.insert(record);

        // 将打卡机打卡数据发送到WeLink
        weLinkIntegrationService.sendToWeLink(record);
    }

    /**
     * 获取授权码
     *
     * @param companyCode
     * @return
     */
    @Override
    public String getAuthCode(String companyCode) {

        // 将打卡机打卡数据发送到WeLink
//        weLinkIntegrationService.sendToWeLink(record);

//        return punchRecordMapper.AuthCode(companyCode);
        return "123456";
    }
}